package com.cfpl.admin_service.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cfpl.admin_service.VO.PageQueryCondition;
import com.cfpl.admin_service.service.GrowerInformationService;
import com.cfpl.project_common.pojo.GrowerInformation;
import com.cfpl.project_common.pojo.PlantingLocation;
import com.cfpl.project_common.result.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Auther: LYP
 * @Date: 2023/10/23 - 10:39
 * @Description: com.cfpl.admin_service.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("/growerInfo")
@Api(tags = "种植户相关接口")
@Slf4j
public class GrowerController {

    @Autowired
    private GrowerInformationService growerService;

    /**
     * 分页查询种植户信息
     * @param currentPage 当前页
     * @param pageSize    每页显示条数
     * @param condition 查询条件   不做处理 将来返回到搜索框
     * @param growerInformation 查询信息
     * 多个参数的Post请求，不要用@RequestBody,
     */
    @ApiOperation("分页查询种植户信息")
    @PostMapping("/pageQueryGrower")
    public BaseResult<PageQueryCondition> pageQueryGrower(int currentPage, int pageSize,String condition,
                                                          GrowerInformation growerInformation){
        LambdaQueryWrapper<GrowerInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                //社会信用代码查询
                .eq(growerInformation.getId() != null,GrowerInformation::getId,growerInformation.getId())
                .like(StrUtil.isNotBlank(growerInformation.getEnterprise()),GrowerInformation::getEnterprise,growerInformation.getEnterprise())
                //模糊查询联系人
                .like(StrUtil.isNotBlank(growerInformation.getContacts()),GrowerInformation::getContacts,growerInformation.getContacts())
                //查询电话
                .eq(StrUtil.isNotBlank(growerInformation.getPhone()),GrowerInformation::getPhone,growerInformation.getPhone())
                //查询邮件
                .eq(StrUtil.isNotBlank(growerInformation.getEmail()),GrowerInformation::getEmail,growerInformation.getEmail())
                //模糊查询地址
                .like(StrUtil.isNotBlank(growerInformation.getAddress()),GrowerInformation::getAddress,growerInformation.getAddress())
//                //加入时间大于查询时间的种植户
                .ge(StrUtil.isNotBlank(growerInformation.getJoinTime()),GrowerInformation::getJoinTime,growerInformation.getJoinTime())
                //种植面积大于给定面积的种植户
                .ge(Objects.nonNull(growerInformation.getPlantArea()),GrowerInformation::getPlantArea,growerInformation.getPlantArea());
        Page<GrowerInformation> page = growerService.page(new Page<>(currentPage, pageSize),queryWrapper);
        PageQueryCondition queryCondition = new PageQueryCondition(page,condition);
        return BaseResult.ok(queryCondition);
    }


    /**
     * 添加种植户信息
     * @param growerInformation 种植户信息
     */
    @ApiOperation("添加种植户信息")
    @PostMapping("/insertGrowerInformation")
    public BaseResult insertGrowerInformation(@RequestBody GrowerInformation growerInformation){
        growerService.save(growerInformation);
        return BaseResult.ok();
    }

    /**
     * 根据ID查询种植户
     * @param growerId id
     */
    @ApiOperation("根据ID查询种植户")
    @GetMapping("/queryGrowerById/{id}")
    public BaseResult<GrowerInformation> queryGrowerById(@PathVariable("id") Long growerId){
        GrowerInformation information = growerService.getById(growerId);
        return BaseResult.ok(information);
    }

    /**
     * 更新种植户信息
     * @param growerInformation 种植户信息
     */
    @ApiOperation("更新种植户信息")
    @PostMapping("/updateGrowerInformation")
    public BaseResult updateGrowerInformation(@RequestBody GrowerInformation growerInformation){
        growerService.updateById(growerInformation);
        return BaseResult.ok();
    }


    /**
     * 根据种植户ID查询旗下的所有种植信息  在PlantLocationController接口中实现了
     */



}
